# -*- coding:utf-8 -*-
import redis

# redisClient = redis.StrictRedis(host = 'localhost', port = 6379, db = 1)
# print redisClient.ping()
# redisClient.set('test',"成功")
# print 'getset',redisClient.get('test')
# print redisClient.dbsize()
# keys = redisClient.keys()
# for key in keys:
#     print keys[0]
# client = redis.StrictRedis(host='localhost', port=6379)
# print client.ping()

class RedisManager(object):
    def __init__(self):
        self.client_unCraw = redis.StrictRedis(host = 'localhost', port = 6379, db = 1)
        self.client_crawed = redis.StrictRedis(host = 'localhost', port = 6379, db = 2)
        self.STATUS_URL_NEW = 0
        self.STATUS_URL_FINISH = 1
    def add_new_url(self, url):
        if url is None:
            return
        key = url
        value_unCraw = self.client_unCraw.get(key)
        value_crawed = self.client_crawed.get(key)
        if (value_unCraw is None) and (value_crawed is None):
            self.client_unCraw.set(key,self.STATUS_URL_NEW)
        if (value_unCraw is None) and (value_crawed != None):
            self.client_crawed.set(key,self.STATUS_URL_FINISH)
    def add_new_urls(self, urls):
        if urls is None or len(urls) == 0:
            return
        for url in urls:
            self.add_new_url(url)

    def has_new_url(self):
        return len(self.client_unCraw.keys()) != 0

    def get_new_url(self):
        keys = self.client_unCraw.keys()
        return keys[0]

    def finish_url(self,url):
        self.client_unCraw.delete(url)
        self.client_crawed.set(url,self.STATUS_URL_FINISH)

    def del_url(self,url):
        self.client_unCraw.delete(url)
